module CPP
class Source
    attr_reader :dependencies, :name, :path, :action
    
    def initialize(path, full_path = true, dep_filter = nil, padding = "")
        @name = File.basename(path, File.extname(path)) + ".o"
        @path = "$(SRC)#{padding}#{File.basename(path)}" if !full_path
        @path = "#{File.basename(path)}" if full_path
        @dependencies = Array.new()
        
        @action = "$(CXX) $(CXXFLAGS) -c "
        @action << "#{@path}"
        
        
        file = File.read(path)
        file.scan(/(#include\s+[<"])(.+)([>"])/) do |start, name, stop|
            if !dep_filter or dep_filter.index(File.basename(name))
                @dependencies.push("$(INC)#{padding}#{name}")
            else
            
            end
        end
    end
    
    def to_s()
        return @name
    end
end

def self.sources_from_dir(path, dep_filter = nil, padding = "")
    sources = Array.new()
    puts dep_filter
    Dir.entries(path).each do |file|
        if File.extname(file).match(/cpp|cc|c/)
            puts file
            sources.push(Source.new(File.join(path, file), false, dep_filter, padding))
        end
    end
    return sources
end

def self.dep_filter_from_dir(path)
    deps = Array.new()
    Dir.entries(path).each do |file|
        if File.extname(file).downcase == ".h" or File.extname(file).match(/hpp|h/)
            puts file
            deps.push(file)
        end
    end
    return deps
end
end

module ToMake
def self.targets_from_sources(io, sources)
    puts "To Make: Targets from sources"
    
    sources.each do |source|
        puts " #{source.name}"
        io.puts "#{source.name}: #{source.path} #{source.dependencies.join(" ")}"
        io.puts "\t#{source.action}"
        io.puts
        
    end
end

def self.main_target_from_sources(io, sources, name)
    puts "To Make: Main target from sources"
    
    io.puts "#{name}: #{sources.join(" ")}"
    io.puts "\t$(CXX) $(LINK) #{sources.join(" ")} -o spelprog2"
    io.puts
end

def self.configure(io)
    io.puts "CXX = g++
FRAMEWORKS = -F../lib/frameworks/ -framework Ogre -framework IOKit -framework Carbon
CXXFLAGS = -O2 $(FRAMEWORKS) -U_OgreOctreePluginExport -D_OgreOctreePluginExport="" -I../lib/include/ -I../lib/include/IrrKlang/ -I../lib/include/QuickGUI/ -I../lib/frameworks/Ogre.framework/Versions/1.7.1/Headers/ -Iinclude/ -Iinclude/gen/ -arch i386 -m32
LINK = $(FRAMEWORKS) ../lib/frameworks/Plugins/Plugin_OctreeSceneManager.dylib -L../lib/bin -L../lib/bin/boost -lboost_filesystem -lquickgui -lstdc++ -lboost_signals -lboost_system -lboost_date_time -lboost_regex -lboost_serialization -lboost_thread -lboost_wserialization -lOIS -lprotobuf -lirrklang -arch i386 -m32
SRC = src/
INC = include/"
    io.puts
end

def self.finish(io)
    io.puts "package_clean:
	rm -f -R sandbox
	
package_update: spelprog2
	cp spelprog2 sandbox/Contents/MacOS

package: spelprog2
	mkdir -p sandbox/Contents/MacOS
	mkdir -p sandbox/Contents/Frameworks
	mkdir -p sandbox/Contents/Resources
	mkdir -p sandbox/Contents/replays
	cp spelprog2 sandbox/Contents/MacOS
	cp ../lib/bin/libquickgui.dylib sandbox/Contents/Frameworks
	cp ../lib/bin/libprotobuf.dylib sandbox/Contents/Frameworks
	cp ../lib/bin/ikpFLAC.dylib sandbox/Contents/MacOS
	cp ../lib/bin/ikpMP3.dylib sandbox/Contents/MacOS
	cp ../lib/bin/libirrklang.dylib sandbox/Contents/Frameworks
	cp -R ../lib/frameworks/Ogre.framework sandbox/Contents/Frameworks
	cp -R ../lib/frameworks/Cg.framework sandbox/Contents/Frameworks
	cp -R ../lib/frameworks/Plugins sandbox/Contents/
	cp -R scripts sandbox/Contents/Resources
	cp -R media sandbox/Contents/Resources
	
clean:
	rm *.o"
	io.puts
end
end

sources = CPP.sources_from_dir("src", CPP.dep_filter_from_dir("include"))
sources.concat CPP.sources_from_dir("src/gen/", CPP.dep_filter_from_dir("include/gen/"), "gen/")
File.open("Makefile", "w") do |f|
    ToMake.configure(f)
    ToMake.main_target_from_sources(f, sources, "spelprog2")
    ToMake.targets_from_sources(f, sources)
    ToMake.finish(f)
end